<?php
namespace app\admin\controller;

use think\Controller;
use think\Log;
use think\Exception;

class Column extends Base
{

    /**
     * 栏目列表
     * @author wcy
     * @version 2017年6月22日11:50:52
     */
    public function columnList(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=$_POST["search"];
                }
                $data=model("Column")->columnList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("columnlist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 添加栏目
     * @author wcy
     * @version 2017年6月22日11:50:52
     */
    public function addColumn(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data["column_name"]=$_POST["column_name"];
                $res=model("Column")->verifColumnName($data["column_name"]);
                if($res){
                    return json(["code"=>-1,"msg"=>"栏目名称已经存在"]);
                }
                $data["column_url"]=$_POST["column_url"];
                $data["status"]=$_POST["status"];
                $res=model("Column")->addColumn($data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"添加成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"添加失败"]);
                }
            }else{
                return view("addcolumn");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            $this->ajaxReturnJson(-1,"系统出错");
        }
    }
    /**
     * 删除栏目
     * @author wcy
     * @version 2017年6月23日14:31:09
     */
    public function delColumn(){
        try{
            $id=$_POST["id"];
            $res=model("Column")->delColumn($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 修改栏目状态
     * @author wcy
     * @version 2017年6月23日14:51:17
     */
    public function editColumnStatus(){
        try{
            $data["status"]=$_POST["status"];
            $id=$_POST["id"];
            $res=model("Column")->editColumn($id,$data);
            if($res!==false){
                return json(["code"=>1,"msg"=>"修改成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"修改失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 修改栏目
     * @author wcy
     * @version 2017年6月26日10:52:02
     */
    public function editColumn(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"];
                $data["column_name"]=$_POST["column_name"];
                $res=model("Column")->verifColumnNameTow($id,$data["column_name"]);
                if($res){
                    return json(["code"=>-1,"msg"=>"栏目名称已经存在"]);
                }
                $data["column_url"]=$_POST["column_url"];
                $data["status"]=$_POST["status"];
                $res=model("Column")->editColumn($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $data=model("Column")->getColumn($id);
                $this->assign("data",$data);
                return view("editcolumn");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"修改失败"]);
        }
    }
}